<template>
	<view>
		<!-- 骨架屏 -->
		<view v-show="loading">
			<view class="skeleton" v-for="i in 5" :key="i">
				<view class="name placeholder"></view>
				<view class="tag placeholder"></view>
				<view class="user">
					<view class="avatar"></view>
					<view class="nickname placeholder"></view>
				</view>
			</view>
		</view>
		<!-- 滑动 -->
		<scroll-view scroll-y enhanced scroll-with-animation @scrolltolower="scrolltolower" v-show="!loading"
			:scroll-top="scrollTop || -1" :show-scrollbar="false" :style="{height: scrollHeight + 'rpx' }">
			<!-- 内容 -->
			<view v-for="i in projlist" :key="i.projId" @click="emitId(i.projId)">
				<view class="card">
					<view class="icon-close">
						<!-- <u-icon name="close" color="#c0c0c0" size="10px"></u-icon> -->
					</view>
					<view class="title marginstyle">
						<text class="name u-line-1">{{i.projName || '--'}}</text>
						<text class="num">{{i.num || 1 }}人</text>
					</view>
					<view class="tag-box marginstyle">
						<text class="tag-item" v-for="j in i.tagslist" :key='i'>{{j}}</text>
					</view>
					<view class="user marginstyle">
						<view class="user-item">
							<view class="avatar">
								<image :src=" i.avatarUrl || '/static/imgs/usered.png'" mode=""></image>
							</view>
							<view class="user-item-content">
								<view class="uname" style="maxwidth: 200rpx">{{i.nickName || '--'}}</view>
								<view class="user-item-time">
									<text class="poptxt">发布于</text>
									<text style="margin: -8rpx 8rpx;color: #424242;">·</text>
									<at-formattime :timetamp="i.createTime || ''" class="time"></at-formattime>
								</view>
								
							</view>
						</view>
						<view class="icon-look">
							<u-icon name="eye-fill" color="#c0c0c0" size="14px" labelSize="12px" :label="i.look">
							</u-icon>
						</view>
					</view>
				</view>
			</view>
			<view style="width: 92%; margin:auto">
				<u-divider textSize="12px" v-if="unLoading > 0" text="没有更多了~"></u-divider>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		projlist,
		burryPoint
	} from '@/common/http.api.js'
	import AtFormattime from '@/components/AtFormattime';
	import {random} from '@/utils/random.js'
	export default {
		components: {
			AtFormattime
		},
		props: {
			type:String,
			total:Array,
			projList: Array,
			scrollHeight: Number,
			nomore: Number,
			loading: Boolean,
			scrollTop: Number,
			reqProjData: Object
		},
		data() {
			return {
				timer: null,
				unLoading: 0,
				projlist: []
			}
		},
		watch: {
			nomore(newVal, oldVal) {
				// console.log('检测传进来的',newVal);
				this.unLoading = newVal
			},		
			'reqProjData.sortId'(newVal, oldVal){
				// 只要点击了分类，必重置
				this.unLoading = 0
			},
			projList(newVal, oldVal){
				this.projlist = newVal
			}
		},
		computed: {
			look() {
				let num = ''
				if (this.looknum > 999) {
					num = this.looknum / 1000 + 'k'
				} else {
					num = num + ''
				}
				return ` · ${num || 0 }`
			}
		},
		methods: {
			emitId(id) {
				this.$emit('getProjId', id)
			},

			async scrolltolower() {
				if (this.unLoading > 0) return
				if(this.type == '1') { // 搜索无需分页
				console.log(this.type,'type');
					this.reqProjData.page ++
				}else{
					this.reqProjData.page = random(this.total[Number(this.reqProjData.sortId)],this.reqProjData.limit) // 随机分页
				}
				
				uni.$loading()
				if (this.timer) clearTimeout(this.timer)
				this.timer = setTimeout(async () => {
					const res = await projlist(this.reqProjData)
					res.data.forEach(async item =>{
						await burryPoint({projId:item.projId,num:1})
					})
					if(this.type == '1'){
						if (res.data.length < this.reqProjData.limit) this.unLoading ++
					}else{
						//第二次渲染之后的效果，是拿请求回来的总数 作比较
						let limitLength = this.projlist.length + res.data.length
						if (limitLength >= this.total[Number(this.reqProjData.sortId)] ) this.unLoading ++
					}
					
					this.projlist = [...this.projlist, ...res.data]
				}, 500)

			},
		}
	}
</script>

<style lang="scss" scoped>
	.skeleton {
		@keyframes loading {

			/* 骨架屏的动画 */
			100% {
				transform: translateX(100%);
			}
		}

		margin: 15rpx auto;
		background-color: #fff;
		width: 700rpx;
		height: 220rpx;
		border: 1rpx solid transparent;
		border-radius: 10rpx;

		.placeholder {
			width: 200rpx;
			margin: 25rpx 25rpx;
			height: 35rpx;
			background-color: #eee;
			border-radius: 20rpx;
		}

		.name::after,
		.tag::after,
		.nickname::after {
			/* 内容区域空状态时追加骨架屏样式 */
			content: '';
			display: block;
			width: 240rpx;
			height: 35rpx;
			transform: translateX(-30%);
			background: linear-gradient(90deg, transparent, rgba(225, 225, 225, 0.753), transparent);
			animation: loading 1.5s infinite;
		}

		.name {
			width: 500rpx
		}

		.tag {
			width: 200rpx;
		}

		.tag::after {
			width: 100rpx;
		}

		.user {
			display: flex;
			align-items: center;
			margin-top: -10rpx;

			.avatar {
				width: 60rpx;
				height: 60rpx;
				border-radius: 30rpx;
				background-color: #eee;
				margin-left: 25rpx;

			}

			.nickname {
				height: 25rpx;
				width: 250rpx;
			}

			.nickname::after {
				height: 25rpx;
				width: 120rpx;
			}

		}
	}

	.marginstyle {
		width: 92%;
		margin: 0 auto;
		margin-top: 20rpx;
	}

	.card {
		margin: 0 auto;
		margin-bottom: 15rpx;
		background-color: #fff;
		width: 700rpx;

		position: relative;
		border-radius: 10rpx;
		display: flex;
		flex-direction: column;

		.icon-close {
			position: absolute;
			right: 8rpx;
			top: 8rpx;
		}

		.title {
			margin-top: 25rpx;
			display: flex;
			justify-content: space-between;

			.name {
				font-weight: bold;
				font-size: 18px;
				max-width: 500rpx;
			}

			.num {
				font-weight: bold;
				color: #5098ff;
				font-size: 20px;
			}
		}

		.tag-box {

			.tag-item {
				margin-right: 10rpx;
				padding: 10rpx;
				font-size: 12px;
				border-radius: 5rpx;
				background-color: #f0f2f5;
				color: #494949;
			}
		}

		.user {
			display: flex;
			justify-content: space-between;
			margin-top: 25rpx;
			padding-bottom: 25rpx;

			.user-item {
				// line-height: 60rpx;
				display: flex;
				.user-item-content{
					display: flex;
					flex-direction: column;
					.user-item-time{
						display: flex;
						margin-top: 3rpx;
					}
				}
				.avatar {
					width: 66rpx;
					height: 66rpx;
					border-radius: 33rpx;
					margin-right: 15rpx;
					background-color: #000;
					overflow: hidden;
					image {
						width: 100%;
						height: 100%;
					}
				}
			}
			
			.uname {
				font-size: 12px;
			}

			.poptxt {
				color: #424242 ;
				font-size: 10px;
			}

			.time {
				font-size: 10px;
				color: #424242 ;
			}

			.icon-look {
				position: absolute;
				bottom: 40rpx;
				right: 30rpx;
			}
		}
	}

	.card:first-child {
		margin-top: 15rpx;
	}
</style>
